package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class PrimaryKeySequenceTest {
	public static void main(String[] args) throws Exception {
		sequenceTest("fixyou");
	}

	private static void sequenceTest(String name) throws Exception {
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		String sql="insert into hh_stu (id,name) values(hh_stu_seq.nextval,?)";
		try{
			con=DBUtil.open();
			//statement对象将来返回的自动生成数据的字段是哪一个
			pst=con.prepareStatement(sql,new String[]{"id"});
			pst.setString(1, name);
			pst.executeUpdate();
			//获取指定字段自动生成的数据 
			rs=pst.getGeneratedKeys();
			if(rs.next()){
				//此处不能使用rs.getInt("id");
				int id=rs.getInt(1);
				System.out.println("刚刚插入的主键值是:"+id);
			}
		}finally{
			DBUtil.close(con, pst, rs);
		}
	}
}
